package com.kalix.dataPlatform.right.biz;
import com.kalix.dataPlatform.right.api.biz.ITwoEquipmentBeanService;
import com.kalix.dataPlatform.right.api.dao.ITwoEquipmentBeanDao;
import com.kalix.dataPlatform.right.api.model.PostRequestUtil;
import com.kalix.dataPlatform.right.entities.TwoEquipmentBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TwoEquipmentBeanServiceImpl extends GenericBizServiceImpl<ITwoEquipmentBeanDao, TwoEquipmentBean> implements ITwoEquipmentBeanService {
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP", "config.ksh.portconf");
    public static String Port = (String) ConfigUtil.getConfigProp("FREEBOARD_PORT", "config.ksh.portconf");

    @Override
    public String getTwoEquipmentValue() {
        String sql = "select two from TwoEquipmentBean two";
        List<TwoEquipmentBean> list = dao.find(sql);
        JSONArray str = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            String uuid = list.get(i).getUuid();
            JSONArray a = new JSONArray();
            for (int j = 10; j > 0; j--) {
                long ms = System.currentTimeMillis();
                DateFormat dateChange = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date end_time = new Date();
                Date start_time = new Date();
                end_time.setTime(ms-1800000 * (j-1));
                start_time.setTime(ms - 1800000 * j);
                String startTime = dateChange.format(start_time);
                String endTime = dateChange.format(end_time);
                String body = "{\"metrics\":[{\"moType\":\"Line\",\"uuid\":\"" + uuid + "\",\"metric\":\"ifPercent\"}," +
                        " {\"moType\":\"Line\",\"uuid\":\"" + uuid + "\",\"metric\":\"ifOctets\"}," +
                        "{\"moType\":\"Line\",\"uuid\":\"" + uuid + "\",\"metric\":\"ifDiscards\"}," +
                        "{\"moType\":\"Line\",\"uuid\":\"" + uuid + "\",\"metric\":\"ifUcastPkts\"}," +
                        "{\"moType\":\"Line\",\"uuid\":\"" + uuid + "\",\"metric\":\"ifNUcastPkts\"}]," +
                        "\"startTime\":\""+startTime+"\",\"endTime\":\""+endTime+"\"}";

                String json = PostRequestUtil.HttpPostWithJson("http://" + Ip + ":" + Port + "/api/v1/record/data", body);
                JSONObject jsonObject = new JSONObject(json);
                JSONArray results = jsonObject.getJSONArray("results");
                JSONArray objData = new JSONArray();
                objData.put(endTime);
                for (int k = 0; k < results.length(); k++) {
                    JSONObject result = results.getJSONObject(k);
                    JSONArray arrayValue = result.getJSONArray("values");
                    JSONArray values = arrayValue.getJSONArray(arrayValue.length()-1);
                    String value = values.getString(1);
                    objData.put(value);
                }
                a.put(objData);
            }
            str.put(a);
        }
        return str.toString();
    }

}
